/**
 * Created by Administrator on 2017/10/25 0025.
 */
layui.use(['element','form','layer','laydate','upload'], function() {
    var form = layui.form;
    var element = layui.element;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var upload = layui.upload;
    //表单校验
    laydate.render({
        elem: '#Time', //指定元素
        range: '~',//来自定义分割字符
        btns: [ 'confirm'],
        done:function (value, date, endDate) {
            var split = value.toString().split("~");
            $("#beginTime").val(split[0]);
            $("#endTime").val(split[1]);
        }
    });

    var uploadInst = upload.render({
        elem: '#butt', //绑定元素
        url: '/upload', //上传接口
        before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#img1').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            if(res.code > 0){
                return layer.msg('上传失败');
            }
            //上传成功
            $("#img").val(res);
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#msg');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    })
    //监听提交
    form.on('submit(formSubmit)', function(data){
        $.ajax({
            url:"/voteItem/save",
            type:"post",
            data:data.field,//layui:表单序列化
            dataType:"json",
            success:function (msg) {
                layer.msg("创建成功！",{time:1000},function () {
                   /* var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭*/
                    parent.location.href="/vote/edit?id="+$("#voteId").val()+"&tabId=2";

                });
            },
            error:function () {
                layer.msg("服务器忙！")
            }
        })
        return false;
    })
})